package com.ken.controller.user;

import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ken.context.BaseContext;
import com.ken.entity.AddressBook;
import com.ken.result.Result;
import com.ken.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("userAddressBookController")
@RequestMapping("/user/addressBook")
@Api(tags = "C端-地址簿接口")
@Slf4j
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址")
    public Result<AddressBook> getAddressBookById(@PathVariable String id){
        AddressBook addressBook = addressBookService.getById(id);
        return Result.success(addressBook);
    }

    @ApiOperation("查询默认地址")
    @GetMapping("/default")
    public Result<AddressBook> getDefaultAddressBook() {
        AddressBook addressBook = new AddressBook();
        Long currentId = BaseContext.getCurrentId();
        addressBook.setIsDefault(1);
        addressBook.setUserId(currentId);
        List<AddressBook> defaultAddress = addressBookService.getCurrentUserAddressBook(addressBook);
        if (defaultAddress!=null && defaultAddress.size()==1) {
            return Result.success(defaultAddress.get(0));
        }
        return Result.error("没有查询到默认地址");
    }

    @ApiOperation("查询当前用户的所有地址")
    @GetMapping("/list")
    public Result<List<AddressBook>> getCurrentUserAddress(){
        AddressBook addressBook = new AddressBook();
        Long currentId = BaseContext.getCurrentId();
        addressBook.setUserId(currentId);
        List<AddressBook> list=addressBookService.getCurrentUserAddressBook(addressBook);
        return Result.success(list);
    }

    @PostMapping
    @ApiOperation("新增地址")
    public Result<String> insertUserAddress(@RequestBody AddressBook addressBook){
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.save(addressBook);
        return Result.success();
    }

    @PutMapping
    @ApiOperation("根据id修改地址")
    public Result<String> updateUserAddress(@RequestBody AddressBook addressBook){
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService
                .saveOrUpdate(
                        addressBook,
                        new LambdaQueryWrapper<AddressBook>()
                                .eq(AddressBook::getUserId,addressBook.getUserId())
                                .eq(AddressBook::getId,addressBook.getId()));
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("根据id删除地址")
    public Result<String> delete(String id){
        addressBookService.removeById(id);
        return Result.success();
    }

    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result<String> setDefaultAddress(@RequestBody AddressBook addressBook){
        addressBookService.setDefault(addressBook);
        return Result.success();
    }

}
